//
//  MyViewController.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/3.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "MyViewController.h"
#import "CSMyView.h"
#import "CZQCustomNavView.h"
#import "LoginView.h"

@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource>

/**
 //数据源
 */
@property(nonatomic,strong)NSMutableArray *dataArr;


/**
 tableView
 */
@property(nonatomic,weak)UITableView *tableView;

/**
 tableView
 */
@property(nonatomic,weak)CSMyView *myViewHeader;

//记录起始高度
@property(nonatomic,assign)CGFloat headHeight;

@property(nonatomic,weak)CZQCustomNavView *navView;

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人中心";
    
    if (@available(iOS 11.0, *)) {
        
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = YES;
        
    }
    
    CZQCustomNavView *navView = [CZQCustomNavView new];
    navView.bgView.hidden = YES;
    self.navView = navView;
    [self.view addSubview:navView];
    [navView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.width.mas_equalTo(self.view);
        make.height.mas_equalTo(adaptNavagationBar);
    }];
    self.navView.titleLable.text = @"个人中心";
    self.navView.titleLable.textColor = CSWhiteColor;
    
    [self tableView];
    
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}


#pragma mark -- 懒加载

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray arrayWithObjects:@"每日签到",@"会员中心",@"金币中心",@"去挣钱",@"充值记录",@"提现记录",@"联系客服", nil];
        
        
        
    }
    
    return _dataArr;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        
        tableView.dataSource=self;
        
        tableView.backgroundColor = [UIColor clearColor];;
        
        tableView.rowHeight = 50;
        tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view);
            
        }];
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        //注册
        [tableView registerClass:[CZQSetTableViewCell class] forCellReuseIdentifier:@"cell"];
        
        CSMyView *myViewHead = [[CSMyView alloc]initWithFrame:CGRectMake(0, -(200 + AdaptStatusBar), SCREENWIDTH, (200 + AdaptStatusBar))];
        self.myViewHeader = myViewHead;
        [tableView addSubview:myViewHead];
        [myViewHead.headBtn addTarget:self action:@selector(login)];
        
        [myViewHead setNeedsLayout];
        [myViewHead layoutIfNeeded];
        self.headHeight = 200 + AdaptStatusBar;
        tableView.contentInset = UIEdgeInsetsMake(self.headHeight,0,0,0);
        myViewHead.frame = CGRectMake(0, -self.headHeight, SCREENWIDTH, self.headHeight);
        tableView.contentOffset = CGPointMake(0, -self.headHeight);
        
        UIView *foot = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 130)];
        UIButton *footView = [[UIButton alloc] init];
        footView.frame = CGRectMake(0, 40, self.view.width , 50);
        [footView setTitle:@"退出登录"];
        footView.backgroundColor = CSColorFromRGB(0xF5F5F5);
        footView.titleLabel.font = kFont15;
        [footView addTarget:self action:@selector(loginOut)];
        [footView setTitleColor:CSColorFromRGB(0x010100)];
        [foot addSubview:footView];
        [footView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.center.mas_equalTo(foot);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(50);
        }];
        tableView.tableFooterView = foot;
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CZQSetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = 0;
    cell.titleLable.text = self.dataArr[indexPath.row];
    cell.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"person_%ld",indexPath.row]];
    
    
    return cell  ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *content = self.dataArr[indexPath.row];
    
    
    
}

#pragma mark - UITableViewDelegate
/**
 * 处理联动
 * 因为要实现下拉头部放大的问题，tableView设置了contentInset，所以试图刚加载的时候会调用一遍这个方法，所以要做一些特殊处理，
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //当前偏移量
    CGFloat yOffset  = scrollView.contentOffset.y;
    
    
    CGRect frame = self.myViewHeader.frame;
    if (yOffset < -self.headHeight) {
        frame.origin.y=yOffset;
        
        frame.size.height=-yOffset;
        
        self.myViewHeader.frame=frame;
        
    }
    
    NSLog(@"%.2f",yOffset);
    
    CGFloat alpha1 = (adaptNavagationBar + yOffset) /adaptNavagationBar;
    
    if (yOffset >  - adaptNavagationBar) {
        self.navView.bgView.hidden = NO;
        self.navView.bgView.alpha = alpha1;
    }else
    {
        self.navView.bgView.hidden = YES;
        self.navView.bgView.alpha = 0;
    }
}


#pragma mark - 登录
-(void)login
{
    LoginView *loginView = [[LoginView alloc]init];
    [loginView showWithAnimation];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
